import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { JsonApiService } from "app/core/api/json-api.service";

declare var $:any;
@Component({
	selector: 'sa-scheduling-page',
	templateUrl: './scheduling-page.component.html',
})
export class SchedulingPageComponent implements OnInit {
	page: any = {
		jobName:'',
		beanClass:'',
		methodName:'',
		cronExpression:'',
		description:'',
		isConcurrent:'1',
		jobStatus:'1'
	};
	page_title:string = '新增';
	pageType:string = 'new';
	schedulingId = null;
	submitted = false;
	curPage = 0;//缓存页码
	validatorOptions = {
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			jobName: {
				validators: {
					notEmpty: {
						message: '请输入任务名称'
					},
					stringLength: {
						max: 30,
						message: '资源类型30字以内'
					}
				}
			},
			beanClass: {
				validators: {
					notEmpty: {
						message: '请输入全限定名称'
					}
				}
			},
			methodName: {
				validators: {
					notEmpty: {
						message: '请输入方法名称'
					}
				}
			},
			cronExpression: {
				validators: {
					notEmpty: {
						message: '请输入corn表达式'
					}
				}
			}
		}

	}
	constructor(private jsonApiService: JsonApiService,private router: Router, private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.schedulingId = queryParams.id;
			this.pageType = queryParams.type;
			this.curPage = queryParams.nowPage;
		});
	}
	
//	提交数据
	submitParam() {
		$('#schedulingForm').bootstrapValidator('validate'); //开启验证
		let isValid = $('#schedulingForm').data('bootstrapValidator').isValid(); //验证是否通过
		if(isValid) {
			this.updata();
		}
	}
	updata(){
		let reqdata,type;
		if(this.pageType === 'new'){
			type = "add";
			reqdata = 'jobName=' + this.page.jobName+'&beanClass='+this.page.beanClass+'&methodName='+this.page.methodName+'&cronExpression='+this.page.cronExpression+'&description='+
			this.page.description+'&jobStatus='+this.page.jobStatus+'&isConcurrent='+this.page.isConcurrent;
		}else if(this.pageType === 'updata'){
			type = "edit";
			reqdata = 'jobName=' + this.page.jobName+'&beanClass='+this.page.beanClass+'&methodName='+this.page.methodName+'&cronExpression='+this.page.cronExpression+'&description='+
			this.page.description+'&jobStatus='+this.page.jobStatus+'&isConcurrent='+this.page.isConcurrent+'&id='+this.schedulingId;
		}
		this.jsonApiService.http("job/"+type+".html", reqdata).subscribe(data => {
			if(data.success){
				this.router.navigate(['/system-config/scheduling'],{ queryParams: {nowPage:this.curPage}});
			}
			this.jsonApiService.message(data.message)
		})
	}
	//	返回资源列表页
	goBack() {
		this.router.navigate(['/system-config/scheduling'],{ queryParams: {nowPage:this.curPage}});
	}
	ngOnInit() {
		if(this.pageType === 'updata') {
			this.page_title = '编辑';
			let reqdata = 'id='+this.schedulingId;
			this.jsonApiService.http("job/scheduleJobInfo.html",reqdata).subscribe(data=> {
				if(data.success){
					for(let item in this.page) {
						this.page[item] = data.data[item];
					}
				}else{
					this.jsonApiService.message(data.message)
				}
			})
		} else {
			return
		}
	}
}